class Breadcrumd {
  constructor() {
    this.stack = [];
    this.maxBreadcrumds = 20;
    // 用户自定义hook
    this.beforePushBreadcrumb = null;
  }

  push(data) {
    if (typeof this.beforePushBreadcrumb === "function") {
      let result = this.beforePushBreadcrumb(this, data);
      if (!result) return;
      this.immediatePush(result);
      return;
    }
    this.immediatePush(data);
  }

  immediatePush(data) {
    if (this.stack.length === this.maxBreadcrumds) {
      this.stack.shift();
    }
    this.stack.push(data);
    this.stack.sort((a, b) => a.time - b.time);
  }
}

export default Breadcrumd;
